package com.elepro.shoppingmall.controller.finance;


import com.elepro.shoppingmall.entity.finance.Finance;
import com.elepro.shoppingmall.entity.finance.SplitDate;

import com.elepro.shoppingmall.entity.finance.YMD;
import com.elepro.shoppingmall.mapper.finance.FinanceMapper;
import com.elepro.shoppingmall.service.finance.FinanceServiceIm;
import com.elepro.shoppingmall.utils.Finance.FinanceUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/finance")
@CrossOrigin
public class FinanceController {

    @Autowired
    private FinanceServiceIm financeServiceIm;

    @Autowired
    private FinanceMapper financeMapper;

    @GetMapping("/getFinanceToDay")
    public SplitDate getFinanceToDay(){
        Date toDay = new Date();
        return financeServiceIm.getFinanceListByToDay(toDay,true);
    }

    @GetMapping("/getFinanceListTo")
    public  SplitDate getFinanceListTo(@RequestParam("type") String type){
//        List<YMD> ymdList = financeMapper.getFinanceListByYear();
        if (type.equals("year"))
        {
            System.out.println(type);
            return financeServiceIm.getFinanceListToYear();

        }
        else if (type.equals("month"))
            return financeServiceIm.getFinanceListToMonth();
        else if (type.equals("day")){
            return financeServiceIm.getFinanceListToDay();
        }
//        System.out.println(type);
        return null;
    }

    @GetMapping("/getFinanceByYMD")
    public SplitDate getFinanceByYMD(@RequestParam("date") String date){

        return financeServiceIm.getFinanceListByToDay(FinanceUtils.myConvert(date),false);
    }

    @PostMapping("/insertFinance")
    public String insertFinanceById(@RequestParam("allMoney") Double allMoney){

        Finance finance = new Finance();
        finance.setTime(new Date());
        finance.setAllMoney(allMoney);
        if (financeServiceIm.insertFinance(finance)==0)
            return "ture";
        return "false";
    }

}
